package cn.edu.pku.ss.lbs.blip;

import com.blipsystems.blipnet.api.blipnode.BlipNodeHandle;
import com.blipsystems.blipnet.api.blipnode.BlipNodeHandleInUseException;
import com.blipsystems.blipnet.api.blipserver.BlipServerConnectionException;

public class BlipNodeLocker extends Thread {
    public static void Lock(BlipNodeHandle handle) throws BlipServerConnectionException, BlipNodeHandleInUseException {
        BlipNodeLocker locker = new BlipNodeLocker(handle);
        Runtime.getRuntime().addShutdownHook(locker);
        handle.lock();
    }

    private BlipNodeHandle handle;

    private BlipNodeLocker(BlipNodeHandle handle) {
        this.handle = handle;
    }

    public void run() {
        try {
            handle.release();
        } catch (BlipServerConnectionException e) {
            e.printStackTrace();
        }
    }
}
